<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 广告管理 控制器
 */
class Advert extends Admin_Controller {

    private $_per_page = 10; //显示条数

    public function __construct() {
        parent::__construct();
        $this->load->model('advert_model');
        log_message('debug', 'Advert Admin Controller Class Initialized');
    }

    /**
     * 列表页面
     */
    public function index() {
        if (IS_POST) {
            $posts = $this->input->post(null, true);
            foreach ($posts['data'] as $id => $post) {
                $save = array();
                $save['vieworder'] = (int)$post['vieworder'];
                $save['status'] = isset($post['status']) ? 1 : 0;
                $this->advert_model->update($id, $save);
            }
            $this->_update_advert_cache();
            $this->show_success('success', 'admin/advert/index');
        } else {
            //搜索
            $get['page'] = $this->input->get('page', true);
            $get['advert_type'] = $this->input->get('advert_type', true);
            $get['ckey'] = $this->input->get('ckey', true);
            $get['descrip'] = $this->input->get('descrip', true);
            $get['status'] = $this->input->get('status', true);

            //查询条件
            $where = array();
            $where['type'] = 1; //广告
            $get['advert_type'] && $where['advert_type'] = $get['advert_type'];
            $get['ckey'] && $get['ckey'] != '全部' && $where['ckey'] = $get['ckey'];
            $get['descrip'] && $where['descrip'] = $get['descrip'];
            $get['status'] && $where['status'] = $get['status'];

            //分页
            $count = $this->advert_model->get_adverts_count($where);
            $config['base_url'] = '?' . http_build_query($where); //基本url
            $config['per_page'] = $this->_per_page; //显示条数
            $config['total_rows'] = $count;
            $this->load->library('pagination', $config);
            list($start, $limit) = Utility::page2limit($get['page'], $this->_per_page);

            $data = array();
            $data['title'] = '广告管理';
            $data['get'] = $get;
            $data['adverts'] = $this->advert_model->get_adverts($limit, $start, $where);
            $data['advert_types'] = $this->advert_model->get_advert_types();
            $data['advert_cates'] = $this->advert_model->get_advert_cates_dropdown();
            $this->load->view('advert/advert_index', $data);
        }
    }

    /**
     * 添加页面
     */
    public function add() {
        $config = $this->input->post('config', true);
        $this->load->library('form_validation');
        //文字广告
        if ($config['type'] == 'txt') {
            $this->form_validation->set_rules('config[title]', '文字内容', 'required');
            $this->form_validation->set_rules('config[link]', '点击链接', 'required');
        } elseif ($config['type'] == 'img') {
            $this->form_validation->set_rules('config[link]', '点击链接', 'required');
        } elseif ($config['type'] == 'code') {
            $this->form_validation->set_rules('config[htmlcode]', '广告代码', 'required');
        } elseif ($config['type'] == 'flash') {
            $this->form_validation->set_rules('config[link]', 'Flash地址', 'required');
            $this->form_validation->set_rules('config[width]', 'Flash宽度', 'required');
            $this->form_validation->set_rules('config[height]', 'Flash高度', 'required');
        }
        $this->form_validation->set_rules('data[descrip]', '广告描述', 'required');
        $this->form_validation->set_rules('data[ckey]', '广告位', 'required');
        if ($this->form_validation->run() == true) {
            $data = $this->input->post('data', true);

            $save = array();
            $save['type'] = 1; //广告
            $save['descrip'] = $data['descrip'];
            $save['ckey'] = $data['ckey'];
            $save['stime'] = strtotime($data['stime']);
            $save['etime'] = strtotime($data['etime']);
            $save['vieworder'] = (int)$data['vieworder'];
            $save['status'] = isset($data['status']) ? 1 : 0;
            $save['config'] = serialize($config);
            if ($save['stime'] > $save['etime']) $this->show_error('开始时间不能大于结束时间');
            $this->advert_model->insert($save);
            $this->_update_advert_cache();
            $this->show_success('success', 'admin/advert/index');
        } else {
            if ($error = $this->form_validation->get_error()) $this->show_error($error);

            $data = array();
            $data['title'] = '广告管理';
            $data['config'] = $this->advert_model->get_default_advert_config();
            $data['advert_cates'] = $this->advert_model->get_advert_cates_dropdown();
            $this->load->view('advert/advert_add', $data);
        }
    }

    /**
     * 编辑页面
     */
    public function edit() {
        $id = $this->input->get_post('id', true);
        $advert = $this->advert_model->get_advert($id);
        if (!$advert) $this->show_error('illegal_request');

        $config = $this->input->post('config', true);
        $this->load->library('form_validation');
        //文字广告
        if ($config['type'] == 'txt') {
            $this->form_validation->set_rules('config[title]', '文字内容', 'required');
            $this->form_validation->set_rules('config[link]', '点击链接', 'required');
        } elseif ($config['type'] == 'img') {
            $this->form_validation->set_rules('config[link]', '点击链接', 'required');
        } elseif ($config['type'] == 'code') {
            $this->form_validation->set_rules('config[htmlcode]', '广告代码', 'required');
        } elseif ($config['type'] == 'flash') {
            $this->form_validation->set_rules('config[link]', 'Flash地址', 'required');
            $this->form_validation->set_rules('config[width]', 'Flash宽度', 'required');
            $this->form_validation->set_rules('config[height]', 'Flash高度', 'required');
        }
        $this->form_validation->set_rules('data[descrip]', '广告描述', 'required');
        $this->form_validation->set_rules('data[ckey]', '广告位', 'required');
        if ($this->form_validation->run() == true) {
            $data = $this->input->post('data', true);

            $save = array();
            $save['type'] = 1; //广告
            $save['descrip'] = $data['descrip'];
            $save['ckey'] = $data['ckey'];
            $save['stime'] = $data['stime'] ? strtotime($data['stime']) : 0;
            $save['etime'] = $data['etime'] ? strtotime($data['etime']) : 0;
            $save['vieworder'] = (int)$data['vieworder'];
            $save['status'] = isset($data['status']) ? 1 : 0;
            $save['config'] = serialize($config);
            if ($save['stime'] > $save['etime']) $this->show_error('开始时间不能大于结束时间');
            $this->advert_model->update($id, $save);
            $this->_update_advert_cache();
            $this->show_success('success', 'admin/advert/index');
        } else {
            if ($error = $this->form_validation->get_error()) $this->show_error($error);

            $advert['stime'] = $advert['stime'] > 0 ? date('Y-m-d', $advert['stime']) : '';
            $advert['etime'] = $advert['etime'] > 0 ? date('Y-m-d', $advert['etime']) : '';
            $config = array();
            $config['type'] = $advert['config']['type'];
            $config[$advert['config']['type']] = $advert['config'];
            $config = array_merge($this->advert_model->get_default_advert_config(), $config);

            $data = array();
            $data['title'] = '广告管理';
            $data['advert'] = $advert;
            $data['config'] = $config;
            $data['advert_cates'] = $this->advert_model->get_advert_cates_dropdown();
            $this->load->view('advert/advert_edit', $data);
        }
    }

    /**
     * 删除处理
     */
    public function dodel() {
        $id = (array)$this->input->get_post('id', true);
        if (empty($id)) $this->show_error('illegal_request');

        $this->advert_model->delete_many($id);
        $this->_update_advert_cache();
        $this->show_success('success', 'admin/advert');
    }

    /**
     * 广告位统计页面
     */
    public function statistics() {
        $data = array();
        $data['title'] = '广告位统计';
        $this->load->view('advert/advert_statistics', $data);
    }

    /**
     *广告位列表页面
     */
    public function cate_index() {
        if (IS_POST) {
            $posts = $this->input->post(null, true);
            foreach ($posts['data'] as $id => $post) {
                $save = array();
                $save['vieworder'] = (int)$post['vieworder'];
                $save['status'] = isset($post['status']) ? 1 : 0;
                $this->advert_model->update($id, $save);
            }
            $this->_update_advert_cache();
            $this->show_success('success', 'admin/advert/cate_index');
        } else {
            //搜索
            $get = array();
            $get['page'] = $this->input->get('page', true);
            $get['descrip'] = $this->input->get('descrip', true);
            $get['status'] = $this->input->get('status', true);

            //查询条件
            $where = array();
            $get['descrip'] && $where['descrip'] = $get['descrip'];
            $get['status'] && $where['status'] = $get['status'];

            //分页
            $count = $this->advert_model->get_advert_cates_count($where);
            $config['base_url'] = '?' . http_build_query($where); //基本url
            $config['per_page'] = $this->_per_page; //显示条数
            $config['total_rows'] = $count;
            $this->load->library('pagination', $config);
            list($start, $limit) = Utility::page2limit($get['page'], $this->_per_page);

            $data = array();
            $data['title'] = '广告位管理';
            $data['get'] = $get;
            $data['advert_cates'] = $this->advert_model->get_advert_cates($limit, $start, $where);
            $this->load->view('advert/advert_cate_index', $data);
        }
    }

    /**
     *广告位增加页面
     */
    public function cate_add() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', '广告位名称', 'required');
        $this->form_validation->set_rules('ckey', '广告位标识符', 'required|alpha_dash|is_unique[advert.ckey]');
        $this->form_validation->set_rules('config[display]', '显示方式', 'required');
        if ($this->form_validation->run() == true) {
            $post = $this->input->post(null, true);

            $_name = str_replace("~\t~", '', $post['name']);
            $_descrip = str_replace("~\t~", '', $post['descrip']);
            $descrip = $_name . "~\t~" . $_descrip;

            $save = array();
            $save['type'] = 0; //广告位
            $save['ckey'] = $post['ckey'];
            $save['descrip'] = $descrip;
            $save['config'] = serialize($post['config']);
            $save['vieworder'] = (int)$post['vieworder'];
            $save['status'] = isset($post['status']) ? 1 : 0;
            $this->advert_model->insert($save);
            $this->_update_advert_cache();
            $this->show_success('success', 'admin/advert/cate_index');
        } else {
            if ($error = $this->form_validation->get_error()) $this->show_error($error);

            $data = array();
            $data['title'] = '广告位管理';
            $this->load->view('advert/advert_cate_add', $data);
        }
    }

    /**
     *广告位编辑页面
     */
    public function cate_edit() {
        $id = $this->input->get_post('id', true);
        $advert_cate = $this->advert_model->get_advert_cate($id);
        if (!$advert_cate) $this->show_error('illegal_request');

        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', '广告位名称', 'required');
        $this->form_validation->set_rules('ckey', '广告位标识符', 'required|alpha_dash');
        if ($advert_cate['ckey'] != $this->input->get_post('ckey', true)) {
            $this->form_validation->set_rules('ckey', '广告位标识符', 'is_unique[advert.ckey]');
        }
        $this->form_validation->set_rules('config[display]', '显示方式', 'required');
        if ($this->form_validation->run() == true) {
            $post = $this->input->post(null, true);

            $_name = str_replace("~\t~", '', $post['name']);
            $_descrip = str_replace("~\t~", '', $post['descrip']);
            $descrip = $_name . "~\t~" . $_descrip;

            $save = array();
            $save['ckey'] = $post['ckey'];
            $save['descrip'] = $descrip;
            $save['config'] = serialize($post['config']);
            $save['vieworder'] = (int)$post['vieworder'];
            $save['status'] = isset($post['status']) ? 1 : 0;
            $this->advert_model->update($id, $save);
            $this->_update_advert_cache();
            $this->show_success('success', 'admin/advert/cate_index');
        } else {
            if ($error = $this->form_validation->get_error()) $this->show_error($error);

            $data = array();
            $data['title'] = '广告位管理';
            $data['advert_cate'] = $advert_cate;
            $this->load->view('advert/advert_cate_edit', $data);
        }
    }

    /**
     *广告位删除处理
     */
    public function cate_dodel() {
        $id = (array)$this->input->get_post('id', true);
        if (empty($id)) $this->show_error('illegal_request');

        $this->advert_model->delete_many($id);
        $this->_update_advert_cache();
        $this->show_success('success', 'admin/advert/cate_index');
    }

    /**
     * 更新广告缓存
     *
     * 广告 广告位 增加|修改|删除时都调用该方法更新缓存
     */
    private function _update_advert_cache() {
        $this->load->service('advert/advert_service');
        $this->advert_service->update_advert_cache();
    }
}

/* End of file admin/advert.php */
/* Location: ./application/controllers/admin/advert.php */
